<template>
  <div class="container">
    <h2 style="text-align: center">修改密码</h2>
    <el-form
      :model="modifyForm"
      status-icon
      :rules="rules"
      ref="modifyForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="原始密码" prop="oldPassword">
        <el-input type="password" v-model="modifyForm.oldPassword" ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input type="password" v-model="modifyForm.newPassword"></el-input>
      </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
        <el-input type="password" v-model="modifyForm.confirmPassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('modifyForm')"
        >提交</el-button>
        <el-button @click="resetForm('modifyForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  <script>
export default {
  data() {
    
    var checkPwd = (value) => {
      let reg = /^(\w){6,20}$/
      return reg.test(value)
    };
    var checkOld = (rule, value, callback) => {
      console.log(value);
        if (value === '') {
          callback(new Error('请输入密码'));
          return
        }
        if (!checkPwd(value)) {
          callback(new Error('密码由6-20位字母数字下划线组成'));
          return
        }
        callback();
      };
    
    var validatePass = (rule, value, callback) => {
      console.log(value);
        if (value === '') {
          callback(new Error('请输入密码'));
          return
        }
        if (!checkPwd(value)) {
          callback(new Error('密码由6-20位字母数字下划线组成'));
          return
        }
          
        if (this.modifyForm.confirmPassword !== '') {
          this.$refs.modifyForm.validateField('confirmPassword');
        }
        callback();
      };
      var validatePass2 = (rule, value, callback) => {
        console.log(value);
        if (value === '') {
          callback(new Error('请再次输入密码'))
          return
        } else if (value !== this.modifyForm.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      modifyForm: {
        oldPassword: "",
        newPassword: "",
        confirmPassword: "",
      },
      rules: {
        oldPassword: [{ validator: checkOld, trigger: "blur" }],
        newPassword: [{ validator: validatePass, trigger: "blur" }],
        confirmPassword: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    modify() {
      this.$refs['modifyForm'].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(modifyForm) {
      this.$refs['modifyForm'].resetFields();
    },
    submitForm(val) {
      this.$refs['modifyForm'].validate((valid) => {
        if (valid) {
          this.$http.put('/updatePwd', {
            oldPwd: this.modifyForm.oldPassword,
            newPwd: this.modifyForm.confirmPassword
          })
          .then(res => {
            res = res.data
            if (!res.status) {
              return this.$message.error(res.message)
            }
            window.sessionStorage.clear();
            this.$router.push("/login");
            this.$message.success(res.message)
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });

      
    }
  },
};
</script>